//
//  WBNavigationController.swift
//  仿新浪微博
//
//  Created by 陈强 on 17/2/8.
//  Copyright © 2017年 强. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    //viewControllers 是被push的控制器，设置他左侧的按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {

        if childViewControllers.count > 0 {
            //隐藏底部tabBar
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器类型
            if let vc = viewController as? WBBaseViewController{
                
                var title = "返回"
                
                //判断控制器的级数，只有一个子控制器的时候，显示栈底控制器的标题
                if childViewControllers.count == 1 {
                    //title 显示首页的标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                //去除自定义的navitem
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title:title, target: self, action: #selector(goback),isBack:true)
            }
        }
        


        super.pushViewController(viewController, animated: true)
    }
    
    //pop返回到上一级控制器
    @objc private func goback() {
        popViewController(animated: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    //隐藏默认的Navigation
        navigationBar.isHidden = true

    }

}
